package com.example.jyapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.jyapplication.R;
import com.example.jyapplication.adapter.FoodListAdapter;
import com.example.jyapplication.bean.FoodListBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;

public class FoodListActivity extends AppCompatActivity{
    private ListView lvFood;
    ImageView ivMy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        initView();
        getData();
        initEvent();
    }

    private void initEvent() {
        // 定义点击事件，跳转到个人主页
        ivMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(FoodListActivity.this,PersonActivity.class);
                startActivity(intent2);
            }
        });
    }


    private void getData() {
        //通过网络访问获取信息
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        // 定义url
        String url = "http://192.168.43.221:8080/18/Menu/all";
        JSONObject jsonObject = new JSONObject();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        // gson解析数据
                        FoodListBean foodListBean = gson.fromJson(jsonObject.toString(),FoodListBean.class);
                        if (foodListBean.isSuccess()){
                            final List list = foodListBean.getList();
                            // 定义适配器
                            FoodListAdapter listAdapter = new FoodListAdapter(list,getApplicationContext());
                            //绑定适配器
                            lvFood.setAdapter(listAdapter);
                            // 设置item的点击事件
                            lvFood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(FoodListActivity.this,FoodActivity.class);
                                    intent.putExtra("id",position+1);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(FoodListActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
        // 绑定控件
        lvFood = findViewById(R.id.lv_foodlist);
        ivMy = findViewById(R.id.iv_my);
    }

}